﻿using System;
using System.Collections.Specialized;
using Eflong.Components;

namespace Eflong.Monitor
{
    internal class MonitorUpdateTask
    {
        private MultiBackgroundWorker multiBackgroundWorker;

        internal HybridDictionary TaskDic { get; set; }

        public MonitorUpdateTask( )
        {
            InitMultiBackgroundWorker( );
        }

        private void InitMultiBackgroundWorker( )
        {
            TaskDic = new HybridDictionary( );
            multiBackgroundWorker = new MultiBackgroundWorker( );

            multiBackgroundWorker.ProgressChanged += new MultiProgressChangedEventHandler( multiBackgroundWorker_ProgressChanged );
            multiBackgroundWorker.RunWorkerCompleted += new MultiRunWorkerCompletedEventHandler( multiBackgroundWorker_RunWorkerCompleted );
            multiBackgroundWorker.DoWork += new MultiDoWorkEventHandler( multiBackgroundWorker_DoWork );
        }

        internal void Cleanup( )
        {
            foreach ( var taskId in TaskDic.Keys )
            {
                TaskItem item = ( TaskItem )TaskDic[ taskId ];
                multiBackgroundWorker.CancelAsync( taskId );
                TaskDic.Remove( taskId );
            }
            multiBackgroundWorker.Dispose( );
            multiBackgroundWorker = null;
        }

        internal void CancleSaveRecords( string taskId )
        {
            multiBackgroundWorker.CancelAsync( taskId );
            TaskItem item = ( TaskItem )TaskDic[ taskId ];

            item.CompletedEventArgs = new MultiRunWorkerCompletedEventArgs( taskId , null , null , true );

            item.EndTime = DateTime.Now;
        }

        /// <summary>
        /// 开始一个新的保存任务
        /// </summary>
        internal void StartUpdating( UpdateWork ua , long nId )
        {
            if ( multiBackgroundWorker != null )
            {
                // Task IDs are Guids.
                string taskId = Guid.NewGuid( ).ToString( );

                TaskItem item = new TaskItem( ) { TaskId = taskId , UserState = ua , StartTime = DateTime.Now , ProgressPercentage = 0 };
                TaskDic.Add( taskId , item );

                multiBackgroundWorker.RunWorkerAsync( taskId , ua );
            }
        }

        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void multiBackgroundWorker_DoWork( object sender , MultiDoWorkEventArgs e )
        {
            UpdateWork ua = ( UpdateWork )e.Argument;
            object taskId = e.TaskId;

            ua.DoWork( );
        }

        private void multiBackgroundWorker_RunWorkerCompleted( object sender , MultiRunWorkerCompletedEventArgs e )
        {
            string taskId = ( string )e.TaskId;

            TaskItem item = ( TaskItem )TaskDic[ taskId ];
            TaskDic.Remove( taskId );

            item.CompletedEventArgs = e;
            item.EndTime = DateTime.Now;
        }

        private void multiBackgroundWorker_ProgressChanged( object sender , MultiProgressChangedEventArgs e )
        {
            string taskId = ( string )e.TaskId;

            TaskItem item = ( TaskItem )TaskDic[ taskId ];
            item.ProgressPercentage = e.ProgressPercentage;
        }
    }
}